package com.lut.web;

import com.lut.pojo.Book;
import com.lut.pojo.Page;
import com.lut.service.BookService;
import com.lut.service.impl.BookServiceImpl;
import com.lut.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @user:dell
 * @date:2020年10月10 16:13
 */
public class BookServlet  extends BaseServlet{
    private BookService bookService=new BookServiceImpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.ParseInt(req.getParameter("pageNo"), 0);
        pageNo+=1;
        //            1.获取请求的对象，封装称为Book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
//        2.调用bookservice.addbook（）保存图书
        bookService.addBook(book);
//        3.跳转图书列表页面
//         req.getRequestDispatcher("/manager/BookServlet?action=list").forward(req,resp);这样会重复提交
        resp.sendRedirect(req.getContextPath()+"/manager/BookServlet?action=page&pageNo="+pageNo);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求的图书编号

        int id = WebUtils.ParseInt(req.getParameter("id"), 0);
//        2.调用bookservic.deleteBookById方法删除图书
        bookService.deleteBookById(id);
//        3.重定向回图书列表页面
        resp.sendRedirect(req.getContextPath()+"/manager/BookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求的参数，封装成book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
//        2.调用  bookService.updateBook()方法，更新图书信息
        bookService.updateBook(book);
//        3.重定向回图书列表页面
        resp.sendRedirect(req.getContextPath()+"/manager/BookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
//        2.把全部图书保存到request域中
        req.setAttribute("books",books);
//        3.请求转发到bookmanager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取请求的图书编号
        int id = WebUtils.ParseInt(req.getParameter("id"), 0);
//        2.调用bookService.queryBookById，获得图书的相关信息
        Book book = bookService.queryBookById(id);
//        3.保存图书信息到request域中
        req.setAttribute("bookmsg",book);
//        4.请求重定向
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);



    }

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.ParseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.ParseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page=bookService.page(pageNo,pageSize);
        page.setUrl("manager/BookServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}

